package com.imnjh.imagepicker.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.imnjh.imagepicker.R;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;

/**
 * Created by Martin on 2017/1/16.
 */

public abstract class BasePickerActivity extends AppCompatActivity {
  protected View contentView;

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (getLayoutResId() != 0) {
      contentView = inflater.inflate(getLayoutResId(), null, false);
    }
    if (contentView != null) {
      setContentView(contentView);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setStatusBarUpperAPI21();
    }
  }

  // 5.0版本以上
  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  private void setStatusBarUpperAPI21() {
    Window window = getWindow();
    //设置透明状态栏,这样才能让 ContentView 向上
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(this.getResources().getColor(R.color.black));

    ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
      //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
      ViewCompat.setFitsSystemWindows(mChildView, false);
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  protected abstract int getLayoutResId();
}
